<?php
/**
 * 
 * @author tangbl
 *
 */
class PublicAction extends CommonAction{
	
	
/**
	 * 初始化
	 * 子类重写_initialize方法需要调用parent::_initialize();
	 */
	public function _initialize() {
	
		$t     = C('VAR_TEMPLATE');
		$theme = $this->_param("$t","","");
		if(empty($theme)){//待处理
			$theme = C('DEFAULT_THEME');
		}
		if(!strpos('|'.C('THEME_LIST'),$theme)){
			$theme = C('DEFAULT_THEME');
		}
		cookie('think_template',$theme,3600);
	
		$NOW_THEME_PATH = TMPL_PATH.$theme.'/';
		$NOW_THEME_URL  = TMPL_URL .$theme.'/';
	
		$this->assign('NOW_THEME_PATH',$NOW_THEME_PATH);
		$this->assign('NOW_THEME_URL' ,$NOW_THEME_URL);
		define('NOW_THEME_NAME', $theme);
	
		$value = get_syscfig_key("webname".LANG_SET);
		$this->assign('webname' ,$value);
	
		//$this->checkMBAC();
		$this->_initmenu();
	}

	//转到应用首页
	public function index(){
		
		redirect(__APP__);
	}
	
	//登陆
	public function login(){
		
		if(IS_POST){
			
			//$verify = $this->_param('yzm',"trim","");
			//if(empty($verify)||$_SESSION['verify'] != md5(strtoupper($verify))){
				//$this->error("验证码输入错误");
			//}
			
			 $member     =  D("Admin");
			 $username   =  $this->_post('username');
        	 $password   =  $this->_post('password'); 
        	 
        	 if(empty($username)||empty($password)){
        	 	$this->error("账号或者密码不能为空");
        	 }
			
			 $map['username'] = $username;
			 $authInfo        = $member->where($map)->find();
			 
			 if(false == $authInfo) {
				 $this->error(L('pbc_validate_no_exsits'));
			 }else{
				if($authInfo['password'] != myencrypt($password) ) {
					$this->error("密码不正确");
				}
				
				$config = F("Hdmin");
				$hdmin  = $this->checkHdminPermission($authInfo,$config);
				if(!$hdmin){
					$this->error('该用户没有掌控中心权限');
				}
			
				$ip		=	get_client_ip();
				$data   = array();
				if($ip){
					import('ORG.Net.IpLocation');
					$Ip = new IpLocation();
					$location = $Ip->getlocation($ip);
					$data['last_location'] = '';
					if($location['country'] && $location['country']!='CZ88.NET') $data['lastLocation'].=$location['country'];
					if($location['area']    && $location['area']!='CZ88.NET') $data['lastLocation'].=' '.$location['area'];
				}
				$data['id']	=	$authInfo['id'];
				$time = time();
				$ip   = get_client_ip();
				$data['last_login_time'] =	$time;
				$data['last_login_ip']	 =	$ip;
				$member->save($data);
				
				$role = M("AdminRole")->find($authInfo['role']);
				session("RoleObjcet",$role);
				session(C('USER_AUTH_KEY') ,$authInfo['id']);
				session(C('USER_AUTH_NAME'),$authInfo['realname']);
				session(C('USER_AUTH_OBJECT'),$authInfo);
				session(C('USER_AUTH_TIME_VIEW'),$role['time_view']);
				session("hdminRole",$authInfo['role']);
				session("last_login_ip",$ip);
				session("last_login_time",$time);
				
			 	$referurl = session(C('REFER_URL'));
				if(empty($referurl)){
					$this->assign("jumpUrl",U('Index/index'));
				}else{
					$this->assign("jumpUrl",$referurl);
					session(C('REFER_URL'),null);
				}

				$this->success("登录成功");
			 }	
		}else{//登陆页面和个人编辑页面
			$userId = session(C('USER_AUTH_KEY'));
			if($userId){
				redirect(U('Index/index'));
			}else{
				$this->display();
			}
		}
	}
	
	//注销操作
	public function logout() {
		unset($_SESSION[C('USER_AUTH_KEY')]);
		unset($_SESSION);
		session_destroy();
		$this->success(L('pbc_login_logoutsucc'),U("Public/login"));
	}
	
	//显示验证码
	public function verify() {
		$type	=	isset($_GET['type'])?$_GET['type']:'gif';
		import("ORG.Util.Image");
		Image::buildImageVerify(4,1,$type);
	}
	
	//显示Qrcode
	public function qrcode() {
		
		vendor("phpqrcode.phpqrcode");
		$url = $this->_param("url","urldecode");
		//$url   = "http://192.18.1.10/".U("Order/detail","id=1");
		$level = 'L';// 纠错级别：L、M、Q、H
		$size  = 4;
		//$path = "images/";//$fileName = $path.$size.'.png';
		QRcode::png($url, false, 'L', 4 , 1);
	}
	
	//查找门店
	public function mapOutlet(){
	
		$type = $this->_param("type","","area");
		$flag = 1;
		$info = "";
		$area = $this->_param("area","intval",0);
	
		if($type == "area"){
	
			$list = mapByArray($area,"area");
	
		}elseif($type == "address"){
	
			$addr = $this->_param("address","urldecode");
			$poin = mapAddressToPoi($addr);
			if($poin){
				$poit = $poin['lng'].",".$poin['lat'];
				$list = mapNearby(array("location"=>$poit));
			}
			//没有找到周边门店,默认读取区门店
			if(empty($list)){
				$list = mapByArray($area,"area");
			}
		}
	
		if(empty($list)){
			$info = "该区域没有找到对应的门店";
			$flag = 0;
		}
	
		$this->ajaxReturn($list,$info,$flag);
	}
	
	private function checkHdminPermission($user,$config){
		
		$roleid = $user["role"];
		$flag   = false;
		
		if(array_key_exists($roleid,$config)){
			$rows  = $config[$roleid];
			$flag  = $rows["hdmin"];
			
			if($user["outletid"]){
				$outlet = M("Outlet")->find($user["outletid"]);
				session("outlet",$outlet);
			}
			
		}
		return $flag;
	}
}
